//
//  LLGStatuseOriginView.swift
//  Weibo30LLG
//
//  Created by 李龙 on 16/8/12.
//  Copyright © 2016年 lilong. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

//原创微博视图
class LLGStatuseOriginView: UIView {
    
    //记录originView底部约束
    var originViewBottomConstraint: Constraint?

    var statuseViewModel: LLGStatuseViewModel? {
    
        //设置空间数据
        didSet {
        
            if let profile = statuseViewModel?.statuse?.user?.profile_image_url {
                
                //头像
                headImageView.sd_setImageWithURL(NSURL(string: profile), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            
            //昵称
            screenNameLabel.text = statuseViewModel?.statuse?.user?.screen_name
            //内容
            contentLabel.text = statuseViewModel?.statuse?.text
            //来源
            sourceLabel.text = statuseViewModel?.source
            //发送微博时间
            timeLabel.text = statuseViewModel?.timeContent
            
            //会员等级
            mbrankImageView.image = statuseViewModel?.mbrankImage
            
            //认证等级
            verifiedTypeImageView.image = statuseViewModel?.verifiedTypeImage
            
            //卸载约束
            originViewBottomConstraint?.uninstall()
            
            if let picUrls = statuseViewModel?.statuse?.pic_urls where picUrls.count > 0 {
                
                pictureView.hidden = false
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originViewBottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatuseTableViewCellMargin).constraint
                })
                
                pictureView.picUrls = picUrls

            
            } else {
            
                pictureView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                   self.originViewBottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatuseTableViewCellMargin).constraint
                })
            }
            
        }
        
            
    }

    //头像
    private lazy var headImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    //认证类型等级
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //用户名
    private lazy var screenNameLabel: UILabel = UILabel(fontSize: 15, color: UIColor.darkGrayColor())
    
    //会员等级
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    //时间
    private lazy var timeLabel: UILabel = UILabel(fontSize: 12, color: UIColor.orangeColor())
    
    //来源
    private lazy var sourceLabel: UILabel = UILabel(fontSize: 12, color: UIColor.lightGrayColor())
    
    //内容
    private lazy var contentLabel: UILabel = {
    
        let label = UILabel(fontSize: 14, color: UIColor.darkGrayColor())
        
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var pictureView: LLGPictureView = {
    
        let view = LLGPictureView()
        view.backgroundColor = self.backgroundColor
        
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)

        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: --搭建界面,设置约束
    private func setupUI() {
    
        backgroundColor = UIColor.whiteColor()
        
        //添加控件
        addSubview(headImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        
        addSubview(pictureView)
        
        //设置约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(self).offset(StatuseTableViewCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        verifiedTypeImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        screenNameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(StatuseTableViewCellMargin)
        }
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(screenNameLabel)
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(StatuseTableViewCellMargin)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImageView.snp_trailing).offset(StatuseTableViewCellMargin)
            make.bottom.equalTo(headImageView)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(StatuseTableViewCellMargin)
            make.bottom.equalTo(timeLabel)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(StatuseTableViewCellMargin)
            make.leading.equalTo(self).offset(StatuseTableViewCellMargin)
            make.trailing.equalTo(self).offset(-StatuseTableViewCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatuseTableViewCellMargin)
            make.leading.equalTo(contentLabel)
        }
        
        //设置空间高度
        self.snp_makeConstraints { (make) -> Void in
            self.originViewBottomConstraint = make.bottom.equalTo(pictureView).offset(StatuseTableViewCellMargin).constraint
        }
        
  
    }
}
